<template>
    <el-form :model="orderinfo" label-width="70px" style="padding: 20px;box-sizing: border-box;">
        <el-form-item label="选择产品">
            <el-button type="success" @click="getInfo('/popupGoodsList')" icon="h-kaidan" class="mb">选择产品</el-button>
            <el-table :data="list" :stripe="true" height="500" show-summary>
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="name" label="商品名称"></el-table-column>
                <el-table-column prop="num" label="数量" width="100">
                    <template scope="scope">
                        <el-input v-model="scope.row.num" size="mini"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="移除">
                    <template scope="scope">
                        <el-button icon="delete2" type="danger" size="small" @click="deleteInfo(scope.$index)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-form-item>
        <el-form-item label="描述">
            <el-input type="textarea" v-model="orderinfo.remarks" placeholder="请输入出入库存描述"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="danger" icon="close" @click="closewin">取消关闭</el-button>
            <el-button type="primary" icon="upload2" @click="onSubmit">确定提交</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
import C from '#/src/common/common.js'
export default {
    name: 'userselectzhiwei',
    data() {
        return {
            list: [],
            orderinfo: {
                remarks: '',
                ordertype: C.getDatas().ordertype
            }
        }
    },
    computed: {
        typename: function() {
            return this.orderinfo.ordertype == 6 ? '入库' : '出库'
        },
        totalnum: function() {
            let totalnum = 0
            this.list.forEach((row) => {
                totalnum += Number(row.num)
            })
            return totalnum
        }
    },
    methods: {
        getInfo: async function(url) {
            var that = this
            let data = await C.openBrowserWindow(url, {}, {
                transparent: true,
                frame: false,
                width: 1000,
                height: 880,
                useSpa: true,
                spaModel: 'organization'
            })
            if (data) {
                for (var i = data.length - 1; i >= 0; i--) {
                    if (!this.inarray(data[i].id, that.list)) {
                        let cacheData = {
                            id: data[i].id,
                            name: data[i].name,
                            num: 1
                        }
                        that.list.push(cacheData)
                    }
                }
            }
        },
        deleteInfo: function(index) {
            this.list.splice(index, 1)
        },
        onSubmit: async function() {
            let goback = false
            let len = this.list.length
            if (len < 1) {
                C.error('请添加' + this.typename + '产品！')
                return false
            }
            for (let i = 0; i < len; i++) {
                if (this.list[i].num <= 0) {
                    goback = true
                }
            }
            if (goback) {
                C.error(this.typename + '产品数量不能小于或等于零！')
                return false
            }
            let orderinfo = {
                totalnum: this.totalnum
            }
            Object.assign(orderinfo, this.orderinfo)
            let data = {
                list: this.list,
                orderinfo: orderinfo
            }
            let res = await C.ajax('/organization/stock/addstockorder', { data: JSON.stringify(data) }, 'post')
            if (res) {
                C.closeBrowserWindow(res)
            }
        },
        closewin: function() {
            C.closewin()
        },
        inarray(id, array) {
            let res = false
            for (var i = array.length - 1; i >= 0; i--) {
                if (id == array[i].id) {
                    res = true
                }
            }
            return res
        }
    }
}
</script>